﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using ESF.General;
using System.Collections;

#endregion


namespace ESF.Web.Handlers
{
	public class CacheSessionCleaner : IHttpHandler
	{
		#region Metodos de IHttpHandler

		public bool IsReusable
		{
			get { return true; }
		}


		public void ProcessRequest(HttpContext pContext)
		{
			StringBuilder resumen = new StringBuilder ();

			bool doClear;


			doClear = TextHelper.GetBool (pContext.Request.QueryString ["Clear"], false);

			resumen.Append ("Current status:<br/>");

			if (pContext.Cache != null)
			{
				resumen.AppendFormat ("<br/><li>Cache vars: {0}</li>", pContext.Cache.Count);
				
				foreach (DictionaryEntry item in pContext.Cache)
				{
					resumen.AppendFormat ("<li>{0} of type {1}</li>", item.Key, item.Value == null ? "NULL" : item.Value.GetType ().ToString ());

					if (doClear)
					{
						pContext.Cache.Remove (item.Key as string);
					}
				}
			}

			if (pContext.Session != null)
			{
				resumen.AppendFormat ("<br/><li>Session vars: {0}</li>", pContext.Session.Count);

				foreach (string item in pContext.Session.Keys)
				{
					resumen.AppendFormat ("<li>{0} of type {1}</li>", item, pContext.Session [item] == null ? "NULL" : pContext.Session [item].GetType ().ToString ());
				}

				if (doClear)
				{
					pContext.Session.Clear ();
				}
			}

			if (pContext.Application != null)
			{
				resumen.AppendFormat ("<br/><li>Application vars: {0}</li>", pContext.Application.Count);

				foreach (string item in pContext.Application.Keys)
				{
					resumen.AppendFormat ("<li>{0} of type {1}</li>", item, pContext.Application [item] == null ? "NULL" : pContext.Application [item].GetType ().ToString ());
				}

				if (doClear)
				{
					pContext.Application.Clear ();
				}
			}

			if (doClear)
			{
				resumen.Append ("<br/><br/>All where clear only for this Application in its Application Pool.");
			}

			pContext.Response.Write (resumen.ToString ());
		}

		#endregion
	}
}
